//
//  FRQFeedbackViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/11/20.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQFeedbackViewController.h"
#import "FeedbackViews.h"
#import "FeedbackTableViewCell.h"
#import "FRQLoginViewController.h"
@interface FRQFeedbackViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *arrList0;
@property (nonatomic,strong) NSMutableArray *arrList1;
@property (nonatomic,strong) NSMutableArray *arrList2;
@property (nonatomic,strong) NSMutableArray *arrList;
@property (nonatomic,strong) FeedbackViews *FeedbackView;
@property (nonatomic,strong) UITableView *tabV;
@property (nonatomic,strong) NSArray *arrTitle;
@end

@implementation FRQFeedbackViewController
- (FeedbackViews *)FeedbackView{
    if (nil==_FeedbackView) {
        _FeedbackView=skXibView(@"FeedbackViews");
        [self.view addSubview:_FeedbackView];
        [_FeedbackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuide);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
            make.right.left.mas_equalTo(0);
        }];
        @weakify(self)
        [[_FeedbackView.btnSend rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self feedback];
        }];
        [[_FeedbackView.btnBack rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self clearAllUserDefaultsData2];
            FRQLoginViewController *view=[[FRQLoginViewController alloc] init];
            BaseNavoigationViewController *nav=[[BaseNavoigationViewController alloc] initWithRootViewController:view];
            [[[UIApplication sharedApplication] delegate] window].rootViewController=nav;
        }];
        
    }
    return _FeedbackView;
}
- (UITableView *)tabV{
    if (nil==_tabV) {
        _tabV=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:(UITableViewStyleGrouped)];
        [self.FeedbackView.viewContain addSubview:_tabV];
        [_tabV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.mas_equalTo(self.FeedbackView.viewContain);
        }];
        _tabV.delegate=self;
        _tabV.dataSource=self;
        self.automaticallyAdjustsScrollViewInsets = NO;
        _tabV.bounces = NO;
        _tabV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabV.estimatedSectionHeaderHeight = 0;
        _tabV.estimatedSectionFooterHeight = 0;
        _tabV.rowHeight = UITableViewAutomaticDimension;
        _tabV.backgroundColor=skUIColorFromRGB(0x1c1f2a);
    }
    return _tabV;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrList0=[[NSMutableArray alloc] init];
    [self.arrList0 addObject:@"0"];
    self.arrList1=[[NSMutableArray alloc] init];
    self.arrList2=[[NSMutableArray alloc] init];
    self.arrList=[[NSMutableArray alloc] init];
    [self.arrList addObject:_arrList0];
    [self.arrList addObject:_arrList1];
    [self.arrList addObject:_arrList2];
    
    self.arrTitle=@[@{@"title":@"1. Areas for App Functionality Improvement:",@"arrTitle":@[@"Wallet Connection",@"Image Format",@"Cropping Tool"]},
    @{@"title":@"2. Aspects of the Device for Improvement:",@"arrTitle":@[@"Battery Life",@"Case Design",@"Accessories"]},
    @{@"title":@"3. Main Reason for Account Cancellation:",@"arrTitle":@[@"I lost my device.",@"I replaced my old device with a new one.",@"I'm no longer using this device."]}];
    
    [self FeedbackView];
    [self tabV];
    [self.tabV reloadData];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] init];
    view.frame=CGRectMake(0, 0, skScreenWidth, 40);
    UILabel *lab=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 40)];
    
    NSDictionary *dic=[self.arrTitle objectAtIndex:section];
    NSString *title=[dic objectForKey:@"title"];
    lab.text=[NSString stringWithFormat:@"%@",title];
    [view addSubview:lab];
    lab.numberOfLines = 2;
    lab.textColor=[UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:14.0];
    return view;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"FeedbackTableViewCell";
    FeedbackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = skXibView(@"FeedbackTableViewCell");
        
    }
    cell.contentView.backgroundColor=skUIColorFromRGB(0x1c1f2a);
    
    NSArray *arr=[self.arrList objectAtIndex:indexPath.section];
    
    NSString *index=[NSString stringWithFormat:@"%ld",indexPath.row];
    if([arr containsObject:index]){
        cell.imageSelect.image=[UIImage imageNamed:@"icom_Select_feedback"];
        
    }else{
        cell.imageSelect.image=[UIImage imageNamed:@"icom_def_feedback 1"];
    }
    NSDictionary *dic=[self.arrTitle objectAtIndex:indexPath.section];
    
    NSArray *arrTitle=[dic objectForKey:@"arrTitle"];
    
    NSString *titleName=[arrTitle objectAtIndex:indexPath.row];
    
    cell.labTitle.text=[NSString stringWithFormat:@"%@",titleName];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMutableArray *arr=[self.arrList objectAtIndex:indexPath.section];
    
    NSString *index=[NSString stringWithFormat:@"%ld",indexPath.row];
    
    if([arr containsObject:index]){
        [arr removeObject:index];
    }else{
        [arr addObject:index];
    }
    [self.arrList replaceObjectAtIndex:indexPath.section withObject:arr];
    [self.tabV reloadData];
}
- (void)feedback {
//    每次启动app调用该接口
    NSMutableArray *mutArr = [NSMutableArray array];
    for(NSInteger i = 0;i < self.arrList.count;i++){
        NSArray *arr = self.arrList[i];
        NSDictionary *d = self.arrTitle[i];
        NSArray *titleArr = [d objectForKey:@"arrTitle"];
        NSMutableDictionary *mutD = [NSMutableDictionary dictionary];
        NSString *issue = [d objectForKey:@"title"];
        for(NSString *str in arr){
            NSString *answers = titleArr[str.integerValue];
            [mutD setObject:issue forKey:@"issue"];
            [mutD setObject:answers forKey:@"answers"];
            [mutArr addObject:mutD];
        }
    }
    
    NSDictionary *dic=@{
        @"feedbackVoList":mutArr,
        @"message":[NSString stringWithFormat:@"%@",self.FeedbackView.txtMessage.text]
    };

    [skNetTools getBody:dic andUrl:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/feedback"] callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        skNetModel *model=[skNetModel mj_objectWithKeyValues:responseObject];
        if (model.code == 200) { //请重新登录
            [self clearAllUserDefaultsData2];
            FRQLoginViewController *view=[[FRQLoginViewController alloc] init];
            BaseNavoigationViewController *nav=[[BaseNavoigationViewController alloc] initWithRootViewController:view];
            [[[UIApplication sharedApplication] delegate] window].rootViewController=nav;
        }else{
            [APP.window makeToast:toString(model.msg)];
        }
    }];
//    [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",@"http://192.168.1.10:8299/tokenshow-biz/",@"app/user/feedback"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
//        NSLog(@"数据=%@",responseObject);
//        skNetModel *model=responseObject;
//        if(model.code==200){
//            [self clearAllUserDefaultsData2];
//            FRQLoginViewController *view=[[FRQLoginViewController alloc] init];
//            BaseNavoigationViewController *nav=[[BaseNavoigationViewController alloc] initWithRootViewController:view];
//            [[[UIApplication sharedApplication] delegate] window].rootViewController=nav;
//        }else{
//            [SkToast SkToastShow:model.msg];
//        }
//    }];
    
}
/// 方法二：清除持久域
-(void)clearAllUserDefaultsData2{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dic = [userDefaults dictionaryRepresentation];
    
    for(id key in dic){
        [userDefaults removeObjectForKey:key];
    }
    [userDefaults synchronize];
}
@end
